package com.frame.work.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.frame.work.entity.TblMessage;
import com.frame.work.mapper.TblMessageMapper;
import com.frame.work.service.TblMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author hewenze
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class TblMessageServiceImpl extends ServiceImpl<TblMessageMapper, TblMessage> implements TblMessageService {

    private final TblMessageMapper tblMessageMapper;

    /**
     * 消息处理
     */

    @Override
    public void insert(TblMessage tblMessage) {
        try {
            int b = tblMessageMapper.insert(tblMessage);
            if (b < 1) {
                log.error("[SensirionLogSave]->[{}]", "保存message日志失败");
            }
        } catch (BadSqlGrammarException e) {
            log.warn("[SensirionLogSave]->[{}]", "保存message日志失败");
        }

    }

    @Override
    public List<TblMessage> selectByTime(Integer pageNum, Integer pageSize, String messageType, String userId) {

        List<TblMessage> tblSecurityLogs = this.tblMessageMapper.selectByTime(pageNum, pageSize, messageType, userId);

        System.out.println(tblSecurityLogs);

        return tblSecurityLogs;
    }

    @Override
    public int deleteAllByUserId(String userId, String messageType) {
        //查询数据库表名
        return this.tblMessageMapper.delete(Wrappers.<TblMessage>lambdaQuery()
                .eq(TblMessage::getUserId, userId).eq(TblMessage::getMessageType, messageType));
    }

    @Override
    public int deleteByIds(List<String> ids) {
        //查询数据库表名
        return this.tblMessageMapper.deleteBatchIds(ids);
    }

    @Override
    public int getUnReadSystemMessageCount(String messageType, String userId) {
        QueryWrapper<TblMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("message_type", messageType).eq("has_read", "0");
        return this.tblMessageMapper.selectCount(queryWrapper);
    }


}
